package com.laikan.reader.article.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.laikan.reader.R;
import com.laikan.reader.article.adapter.holder.*;
import com.laikan.reader.core.bean.BookFocusBean;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/10/21.
 */
public class BookFocusAdapter extends RecyclerView.Adapter implements View.OnClickListener, View.OnLongClickListener {

    public static final int ITEM_TYPE_BANNER = 0;
    public static final int ITEM_TYPE_TEXT = 1;
    public static final int ITEM_TYPE_CONTENT = 2;
    public static final int ITEM_TYPE_IMG = 3;
    public static final int ITEM_TYPE_FOOTER = 4;
    private LayoutInflater mLayoutInflater;
    private OnItemLongClickListener onItemLongClickListener = null;
    private OnItemClickListener onItemClickListener = null;
    private boolean mIsShowFooterView = false;

    private Context mContext;
    private List<BookFocusBean.BookFocusDataList> bookFocusDataLists;
    private ArrayList<BookFocusBean.Banner> bannerList;

    public BookFocusAdapter(Context context, List<BookFocusBean.BookFocusDataList> list, ArrayList<BookFocusBean.Banner> banner) {
        this.mLayoutInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.bookFocusDataLists = list;
        this.bannerList = banner;
    }

    @Override
    public int getItemCount() {
        //        if(bookFocusDataLists.size() > 0 && mIsShowFooterView == false){
        //           return bookFocusDataLists.size() + 1;
        //        }else if (bookFocusDataLists.size() > 0 && mIsShowFooterView == true) {
        //            return bookFocusDataLists.size() + 2;
        //        }
        return getCurrentItemCount();
    }

    /**
     * 当banner与footerview同时显示是，adapter集合长度加2,
     * 当banner与footerview只有其中之一显示时，adapter集合长度加1,
     * 当banner与footerview都需要隐藏时，adapter集合不额外增加集合长度
     * @return 返回adapter item总长度
     */
    public int getCurrentItemCount() {
        if (bookFocusDataLists.size() > 0 && mIsShowFooterView == true && bannerList.size() > 0) {
            return bookFocusDataLists.size() + 2;
        }else if (bookFocusDataLists.size() > 0
                  && ((mIsShowFooterView == true && bannerList.size() <= 0) || (mIsShowFooterView == false && bannerList.size() > 0))) {
            return bookFocusDataLists.size() + 1;
        }else {
            return bookFocusDataLists.size();
        }
    }

    public void setIsShowFooterView(boolean isShowFooterView) {
        this.mIsShowFooterView = isShowFooterView;
        this.notifyDataSetChanged();
    }

    /**
     * 确定数据集合的位置，如果没有banner，数据集合位置从adagper,0 item位置开始，
     * 如果有banner,数据集合位置从adapter,1 item位置开始
     * @param position
     * @return
     */
    public int getDataPositon(int position) {
        if (bannerList.size() > 0 && position > 0) {
            return position - 1;
        }else {
            return position;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && bannerList.size() > 0) {
            return ITEM_TYPE_BANNER;
        }else if (mIsShowFooterView == true && position == getCurrentItemCount() - 1) {
            return ITEM_TYPE_FOOTER;
        }else if (Integer.parseInt(bookFocusDataLists.get(getDataPositon(position)).getType()) == ITEM_TYPE_TEXT) {
            return ITEM_TYPE_TEXT;
        }else if (Integer.parseInt(bookFocusDataLists.get(getDataPositon(position)).getType()) == ITEM_TYPE_CONTENT) {
            return ITEM_TYPE_CONTENT;
        }else if (Integer.parseInt(bookFocusDataLists.get(getDataPositon(position)).getType()) == ITEM_TYPE_IMG) {
            return ITEM_TYPE_IMG;
        }
        return 0;
    }

    public void addData(ArrayList<BookFocusBean.BookFocusDataList> data, ArrayList<BookFocusBean.Banner> banners) {
        this.bookFocusDataLists.addAll(data);
        this.notifyDataSetChanged();
    }

    public void updateData(ArrayList<BookFocusBean.BookFocusDataList> data, ArrayList<BookFocusBean.Banner> banner) {
        this.bookFocusDataLists = data;
        this.bannerList = banner;
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        View view;

        switch (viewType) {
            case ITEM_TYPE_CONTENT:
                view = mLayoutInflater.inflate(R.layout.bookfocus_content_item, parent, false);
                view.setOnClickListener(this);
                view.setOnLongClickListener(this);
                viewHolder = new ContentHolder(view);
                break;
            case ITEM_TYPE_TEXT:
                view = mLayoutInflater.inflate(R.layout.bookfocus_text_item, parent, false);
                view.setOnClickListener(this);
                view.setOnLongClickListener(this);
                viewHolder = new TextHolder(view);
                break;
            case ITEM_TYPE_IMG:
                view = mLayoutInflater.inflate(R.layout.bookfocus_img_item, parent, false);
                view.setOnClickListener(this);
                view.setOnLongClickListener(this);
                viewHolder = new ImageHolder(view);
                break;
            case ITEM_TYPE_FOOTER:
                view = mLayoutInflater.inflate(R.layout.item_comment_foot_view, parent, false);
                //                view.setOnClickListener(this);
                //                view.setOnLongClickListener(this);
                viewHolder = new FooterHolder(view);
                break;
            case ITEM_TYPE_BANNER:
                view = mLayoutInflater.inflate(R.layout.bookfocus_banner_item, parent, false);
                //                view.setOnClickListener(this);
                //                view.setOnLongClickListener(this);
                viewHolder = new BannerHolder(view, mContext);
                break;
            default:
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        BookFocusBean.BookFocusDataList bean = null;
        if(mIsShowFooterView == false ){
            bean = bookFocusDataLists.get(getDataPositon(position));
        }else if(position < getCurrentItemCount() - 1){
            bean = bookFocusDataLists.get(getDataPositon(position));
        }
        if (holder instanceof ContentHolder) {
            ((ContentHolder)holder).getRootView().setTag(bean);
            ((ContentHolder)holder).setData(bean);
        }else if (holder instanceof TextHolder) {
            ((TextHolder)holder).getRootView().setTag(bean);
            ((TextHolder)holder).setData(bean);
        }else if (holder instanceof ImageHolder) {
            ((ImageHolder)holder).getRootView().setTag(bean);
            ((ImageHolder)holder).setData(bean);
        }else if (holder instanceof FooterHolder) {
            // ((ImageHolder)holder).getRootView().setTag(bean);
            // ((ImageHolder)holder).setData(bean);
        }else if (holder instanceof BannerHolder) {
//            ((BannerHolder)holder).getRootView().setTag(bean);
            ((BannerHolder)holder).setData(bannerList);
        }
    }

    @Override
    public void onClick(View view) {
        if (onItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            onItemClickListener.onItemClick(view, view.getTag());
        }
    }

    @Override
    public boolean onLongClick(View view) {
        return onItemLongClickListener != null && onItemLongClickListener.onLongClick(view, view.getTag());
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.onItemLongClickListener = listener;
    }

    public interface OnItemClickListener {

        void onItemClick(View view, Object data);
    }

    public interface OnItemLongClickListener {

        boolean onLongClick(View view, Object data);
    }
}
